<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>websocket</title>
</head>
<body>
<script>
    var socket;
    // 判断当前浏览器是否支持websocket编程
    if (window.WebSocket) {
        // go on
        socket = new WebSocket("ws://localhost:7000/hello")
        // ev收到服务器端发送的消息
        socket.onmessage = function (ev) {
            var rt = document.getElementById('responseText')
            rt.value = rt.value + "\n" + ev.data
        }
        // 相当于连接开启
        socket.onopen = function (ev) {
            var rt = document.getElementById('responseText')
            rt.value = '连接开启~'
        }
        socket.onclose = function (ev) {
            var rt = document.getElementById('responseText')
            rt.value = rt.value + '\n' + '连接关闭~'
        }
    } else {
        alert("您的浏览器太菜了，不支持websocket")
    }

    function send(message) {
        // 发送消息到服务器
        if (!window.socket) {
            return
        }
        if (socket.readyState == WebSocket.OPEN) {
            socket.send(message);
        }
    }
</script>
    <form onsubmit="return false">
        <textarea name="message" style="height: 300px;width: 300px" ></textarea>
        <input type="button" value="发送消息" onclick="send(this.form.message.value)">
        <textarea id="responseText" style="height: 300px;width: 300px"></textarea>
        <input type="button" value="清空内容" onclick="document.getElementById('responseText').value=''">
    </form>
</body>
</html>